package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.facade.FacadeParamFieldBo;
import com.cw.jeeyt.service.converter.facade.FacadeParamFieldConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeParamFieldDao;
import com.cw.jeeyt.dao.model.facade.FacadeParamFieldDo;
import com.cw.jeeyt.dao.query.facade.FacadeParamFieldQuery;
import com.cw.jeeyt.service.facade.FacadeParamFieldService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口参数字段接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeParamFieldServiceImpl implements FacadeParamFieldService {
    @Autowired
    private FacadeParamFieldDao facadeParamFieldDao;

    /**
     * 新增接口参数字段
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeParamField(FacadeParamFieldBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeParamFieldDao.insertBySelective(FacadeParamFieldConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新接口参数字段
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeParamField(FacadeParamFieldBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeParamFieldDao.updateBySelective(FacadeParamFieldConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除接口参数字段
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeParamField(FacadeParamFieldBo request) {
        if (request == null) {
            return false;
        }
        return facadeParamFieldDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询接口参数字段
     *
     * @param request 条件
     * @return PageInfo<FacadeParamFieldBo>
     */
    @Override
    public PageInfo<FacadeParamFieldBo> selectPage(FacadeParamFieldBo request, PageParam pageParam) {
        PageInfo<FacadeParamFieldDo> pageInfo = facadeParamFieldDao.selectPage(FacadeParamFieldQuery.builder()
            .id(request.getId())
            .fieldCode(request.getFieldCode())
            .fieldType(request.getFieldType())
            .required(request.getRequired())
            .maxLength(request.getMaxLength())
            .minLength(request.getMinLength())
            .fieldDesc(request.getFieldDesc())
            .fieldDemo(request.getFieldDemo())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(),pageParam);
        List<FacadeParamFieldBo> facadeParamFields = FacadeParamFieldConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(facadeParamFields)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("required", "std_zb_yn");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(facadeParamFields)),
                dictMap);
            facadeParamFields = JSON.parseArray(jsonArray.toJSONString(), FacadeParamFieldBo.class);
        }
        return new PageInfo<>(pageInfo, facadeParamFields);
    }

    /**
     * 查询接口参数字段详情信息
     *
     * @param request 条件
     * @return FacadeParamFieldBo
     */
    @Override
    public FacadeParamFieldBo queryFacadeParamFieldDetail(FacadeParamFieldBo request) {
        if (request == null) {
            return null;
        }
        return FacadeParamFieldConverter.do2Bo(facadeParamFieldDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询接口参数字段
     *
     * @param request 条件
     * @return List<FacadeParamFieldBo>
     */
    @Override
    public List<FacadeParamFieldBo> queryFacadeParamFieldList(FacadeParamFieldBo request) {
        if (request == null) {
            return null;
        }
        return FacadeParamFieldConverter.doList2BoList(facadeParamFieldDao.selectByExample(
            FacadeParamFieldQuery.builder()
                .id(request.getId())
                .fieldCode(request.getFieldCode())
                .fieldType(request.getFieldType())
                .required(request.getRequired())
                .maxLength(request.getMaxLength())
                .minLength(request.getMinLength())
                .fieldDesc(request.getFieldDesc())
                .fieldDemo(request.getFieldDemo())
                .createBy(request.getCreateBy())
                .createDate(request.getCreateDate())
                .updateBy(request.getUpdateBy())
                .updateDate(request.getUpdateDate())
                .build()));
    }

    /**
     * 根据key查询接口参数字段
     *
     * @param id
     * @return
     */
    @Override
    public FacadeParamFieldBo selectByKey(Long id) {

        return FacadeParamFieldConverter.do2Bo(facadeParamFieldDao.selectByKey(id));
    }
}
